package com.wx.springboot.step6.controller;

import com.wx.springboot.step6.common.api.CommonResult;
import com.wx.springboot.step6.es.document.EsProduct;
import com.wx.springboot.step6.service.EsProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 搜索商品管理Controller
 */
@RestController
@Api(tags = "搜索商品管理")
@RequestMapping("/es/product")
public class EsProductController {

    @Autowired
    private EsProductService esProductService;

    @ApiOperation(value = "导入所有数据库中商品到ES")
    @PostMapping("/import")
    public CommonResult<Integer> importAll() {
        int count = esProductService.importAll();
        return CommonResult.success(count);
    }

    @ApiOperation(value = "根据id删除商品")
    @PostMapping("/delete/{id}")
    public CommonResult<Object> delete(@PathVariable Long id) {
        esProductService.delete(id);
        return CommonResult.success(null);
    }

    @ApiOperation(value = "根据id批量删除商品")
    @PostMapping("/delete/batch")
    public CommonResult<Object> delete(@RequestParam("ids") List<Long> ids) {
        esProductService.delete(ids);
        return CommonResult.success(null);
    }

    @ApiOperation(value = "根据id创建商品")
    @PostMapping("/create/{id}")
    public CommonResult<EsProduct> create(@PathVariable Long id) {
        EsProduct esProduct = esProductService.create(id);
        return CommonResult.success(esProduct);
    }

    @ApiOperation(value = "商品搜索")
    @GetMapping("/search")
    public CommonResult<List<EsProduct>> search(@RequestParam(required = false) String name) {
        List<EsProduct> list = esProductService.search(name);
        return CommonResult.success(list);
    }

}
